package com.libraries.view;

import com.example.libraries.R;
import com.facebook.drawee.view.SimpleDraweeView;
import com.libraries.base.BaseApplication;
import com.libraries.helper.ImgHelper;
import com.libraries.helper.Logger;
import com.libraries.view.listener.INothingListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

@SuppressLint("InlinedApi")
public class NothingView implements OnClickListener {
	private INothingListener nothingListener;
	private SimpleDraweeView iv_nothing;

	private View v_nothing;
	private View v_override;
	private TextView tv_nothing;

	private String nothing, loading;
	private int nothingRes, loadingRes;
	private boolean isLoading = false;

	public NothingView(Context activity, View v_override) {
		this.v_override = v_override;
		initNothingView(activity,false);
	}
	
	public NothingView(Context activity, View v_override,boolean fillParent) {
		this.v_override = v_override;
		initNothingView(activity,fillParent);
	}

	private void initNothingView(Context activity,boolean fillParent) {
		loadingRes = R.drawable.ic_gif_loading;
		nothingRes = R.drawable.ic_nothing_data;
		nothing = BaseApplication.application.getString(R.string.nothing_none);
		loading = BaseApplication.application.getString(R.string.nothing_loading);

		LayoutInflater layoutInflater = LayoutInflater.from(activity);
		v_nothing = (RelativeLayout) layoutInflater.inflate(R.layout.nothing_layout, null);

		iv_nothing = (SimpleDraweeView) v_nothing.findViewById(R.id.iv_nothing);
		tv_nothing = (TextView) v_nothing.findViewById(R.id.tv_nothing);
		v_nothing.findViewById(R.id.ll_nothing).setOnClickListener(this);

		RelativeLayout.LayoutParams lp=null;
		if (fillParent) {
			lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
		}
		else{
			lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
		}
		lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		v_nothing.setLayoutParams(lp);
		ViewGroup parent = (ViewGroup) v_override.getParent();
		parent.addView(v_nothing);
		hideNothing();
	}

	public void setNothingListener(INothingListener nothingListener) {
		this.nothingListener = nothingListener;
	}

	/**
	 * 设置背景图片
	 * 
	 * @param drawableRes
	 */
	public void setBackgroundRes(int drawableRes) {
		if (drawableRes > 10000) {
			v_nothing.setBackgroundResource(drawableRes);
		}
	}

	/**
	 * 设置背景颜色
	 * 
	 * @param gbColor
	 */
	public void setBackgroundColor(int gbColor) {
		if (gbColor != 0) {
			v_nothing.setBackgroundColor(gbColor);
		}
	}

	/**
	 * 设置默认无数据状态提示图片
	 * 
	 * @param nothingRes
	 */
	public void setNothingRes(int nothingRes) {
		this.nothingRes = nothingRes;
	}

	/**
	 * 设置默认加载状态提示图片
	 * 
	 * @param loadingRes
	 */
	public void setLoadingRes(int loadingRes) {
		this.loadingRes = loadingRes;
	}

	/**
	 * 设置默认无数据状态提示信息
	 * 
	 * @param nothing
	 */
	public void setNothingText(String nothing) {
		if (nothing == null) {
			nothing = "";
		}
		this.nothing = nothing;
	}

	/**
	 * 设置默认加载提示信息
	 * 
	 * @param loading
	 */
	public void setLoadingText(String loading) {
		if (loading == null) {
			loading = "";
		}
		this.loading = loading;
	}

	/**
	 * 显示无数据状态
	 * 
	 * @param gbColor
	 *            背景颜色
	 */
	public void showLoading() {
		v_nothing.setVisibility(View.VISIBLE);
		iv_nothing.setVisibility(View.VISIBLE);
		tv_nothing.setVisibility(View.VISIBLE);
		tv_nothing.setText(loading);
		ImgHelper.loadResByFresco(iv_nothing, loadingRes, null);
		changeNothing(true);
	}

	/**
	 * 显示无数据状态
	 * 
	 * @param gbColor
	 *            背景颜色
	 */
	public void showLoading(String loading) {
		v_nothing.setVisibility(View.VISIBLE);
		iv_nothing.setVisibility(View.VISIBLE);
		tv_nothing.setVisibility(View.VISIBLE);
		if (loading != null) {
			tv_nothing.setText(loading);
		} else {
			tv_nothing.setText(this.loading);
		}
		if (loadingRes == -1) {
			iv_nothing.setVisibility(View.GONE);
		} else {
			iv_nothing.setVisibility(View.VISIBLE);
			ImgHelper.loadResByFresco(iv_nothing, loadingRes, null);
		}
		changeNothing(true);
	}

	/**
	 * 显示无数据状态
	 * 
	 * @param nothing
	 */
	public void showNothing(String nothing) {
		v_nothing.setVisibility(View.VISIBLE);
		tv_nothing.setVisibility(View.VISIBLE);
		if (nothing != null) {
			tv_nothing.setText(nothing);
		} else {
			tv_nothing.setText(this.nothing);
		}
		if (nothingRes == -1) {
			iv_nothing.setVisibility(View.GONE);
		} else {
			iv_nothing.setVisibility(View.VISIBLE);
			ImgHelper.loadResByFresco(iv_nothing, nothingRes, null);
		}
		changeNothing(true);
	}

	/**
	 * 显示无数据状态
	 * 
	 * @param nothing
	 */
	public void showNothing() {
		v_nothing.setVisibility(View.VISIBLE);
		tv_nothing.setVisibility(View.VISIBLE);
		tv_nothing.setText(this.nothing);
		if (nothingRes == -1) {
			iv_nothing.setVisibility(View.GONE);
		} else {
			iv_nothing.setVisibility(View.VISIBLE);
			ImgHelper.loadResByFresco(iv_nothing, nothingRes, null);
		}
		changeNothing(true);
	}

	/**
	 * 隐藏无数据状态
	 */
	public void hideNothing() {
		changeNothing(false);
	}

	private void changeNothing(final boolean isNothing) {
		isLoading = false;
		new Handler().post(new Runnable() {
			
			@Override
			public void run() {
				if (isNothing) {
					v_nothing.setVisibility(View.VISIBLE);
					v_override.setVisibility(View.GONE);
				} else {
					v_nothing.setVisibility(View.GONE);
					v_override.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		Logger.log(this, "onClick", nothingListener);
		if (nothingListener != null) {
			showLoading();
			if (isLoading) {
				return;
			}
			isLoading = true;
			new Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					nothingListener.doSomething();
				}
			}, 500);
		}
	}

}
